import java.util.ArrayList;
import java.util.Collections;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2024-12-03
 * Time: 12:25
 */
public class test1 {
    /*
    NewCoder BM12 单链表的排序
    给定一个节点数为n的无序单链表，对其按升序排序。
     */

    public ListNode sortInList (ListNode head) {
        // write code here
        if(head==null || head.next==null){
            return head;
        }
        ArrayList<Integer> nums = new ArrayList();
        ListNode cur = head;
        while(cur!=null){
            nums.add(cur.val);
            cur = cur.next;
        }
        cur = head;
        Collections.sort(nums);
        for(int i=0;i<nums.size();i++){
            cur.val = nums.get(i);
            cur = cur.next;
        }
        return head;
    }

}
